package com.rongshu.view.userInfo.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.rongshu.eventbus.JPushMessage;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.penpal.activity.PenpalDetailActivity;
import com.rongshu.view.penpal.activity.PostDetailsActivity;
import com.rongshu.view.userInfo.activity.AddVCertificationActivity;
import com.rongshu.view.userInfo.activity.AtMeListActivity;
import com.rongshu.view.userInfo.activity.CardBagActivity;
import com.rongshu.view.userInfo.activity.CommentMeListActivity;
import com.rongshu.view.userInfo.activity.DirectChatActivity;
import com.rongshu.view.userInfo.activity.DirectMessageListActivity;
import com.rongshu.view.userInfo.activity.FansActivity;
import com.rongshu.view.userInfo.activity.MessageClassifyActivity;
import com.rongshu.view.userInfo.entity.DirectMessage;
import com.rongshu.view.userInfo.entity.ReceiveMessage;
import com.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;

import cn.jpush.android.api.JPushInterface;

public class JPushMessageRecevier extends BroadcastReceiver {

    //tag
    private final String tag = JPushMessageRecevier.class.getSimpleName();
    //intent
    private Intent mIntent = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                LogUtil.i(tag, "[MyReceiver] 接收Registration Id : " + regId);
                UserInfoUtils.saveRegistrationId(regId);
            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                LogUtil.i(tag, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                LogUtil.i(tag, "[MyReceiver] 接收到推送下来的通知");
                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
                LogUtil.i(tag, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
                LogUtil.i(tag, "[MyReceiver] 推送下来的附加字段: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
                EventBus.getDefault().post(new JPushMessage());
            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                LogUtil.i(tag, "[MyReceiver] 用户点击打开了通知");
                String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
                if (TextUtils.isEmpty(extras)) {
                    return;
                }
                ReceiveMessage receiveMessage = new Gson().fromJson(extras, ReceiveMessage.class);
                //type
                int type = receiveMessage.getType();
                //type_obj
                int type_obj = receiveMessage.getType_obj();
                switch (type) {
                    case Constant.MESSAGE_LETTER://私信
                        mIntent = new Intent(context, DirectChatActivity.class);
                        DirectMessage.ResponseBean responseBean = new DirectMessage.ResponseBean();
                        responseBean.setNewSend_userid(receiveMessage.getId());
                        mIntent.putExtra(Constant.INTENT_DATA, responseBean);
                        context.startActivity(mIntent);
                        break;
                    case Constant.MESSAGE_AT://@
                        if (type_obj == 2) {//帖子
                            mIntent = new Intent(context, PostDetailsActivity.class);
                            mIntent.putExtra(Constant.POST_ID, receiveMessage.getId());
                            mIntent.putExtra("showJoinIcon", true);
                        } else {//文章
                            mIntent = new Intent(context, AtMeListActivity.class);
                        }
                        context.startActivity(mIntent);
                        break;
                    case Constant.MESSAGE_COMMENT://评论
                        if (type_obj == 2) {//帖子
                            mIntent = new Intent(context, PostDetailsActivity.class);
                            mIntent.putExtra(Constant.POST_ID, receiveMessage.getId());
                            mIntent.putExtra("showJoinIcon", true);
                        } else {//文章
                            mIntent = new Intent(context, CommentMeListActivity.class);
                        }
                        context.startActivity(mIntent);
                        break;
                    case Constant.MESSAGE_PRAISE://赞
                    case Constant.MESSAGE_ARTICLE://关注人发布文章
                    case Constant.MESSAGE_CONTRAIBUTE_SUCCESS://文章审核通过
                        if (type_obj == 2) {//帖子
                            mIntent = new Intent(context, PostDetailsActivity.class);
                            mIntent.putExtra(Constant.POST_ID, receiveMessage.getId());
                            mIntent.putExtra("showJoinIcon", true);
                        } else {//文章
                            ArticleDetaisRequest request = new ArticleDetaisRequest();
                            request.setContribute_id(receiveMessage.getId());
                            request.setType(Constant.FROM_HISTORY);
                            request.setTopic_id(0);
                            mIntent = new Intent(context, SingleArticleDetailsActivity.class);
                            mIntent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, request);
                        }
                        context.startActivity(mIntent);
                        break;
                    case Constant.MESSAGE_EVENT://登录送卡
                        mIntent = new Intent(context, CardBagActivity.class);
                        context.startActivity(mIntent);
                        break;
                    case Constant.MESSAGE_PENPAL://加入笔友会、移交会长（副会长）
                        mIntent = new Intent(context, PenpalDetailActivity.class);
                        mIntent.putExtra(Constant.PENPAL_ID, receiveMessage.getId());
                        context.startActivity(mIntent);
                        break;
                    case Constant.MESSAGE_CONTRIBUTE_FAILD://退稿 TODO: 2019/8/16 无法获取退稿信内容
                        mIntent = new Intent(context, MessageClassifyActivity.class);
                        context.startActivity(mIntent);
                        break;
                    case Constant.MESSAGE_V_FAILD://加v失败
                        mIntent = new Intent(context, AddVCertificationActivity.class);
                        context.startActivity(mIntent);
                        break;
                    case Constant.MESSAGE_ATTENTION://被关注
                        if (UserInfoUtils.getUserInfo() != null) {
                            mIntent = new Intent(context, FansActivity.class);
                            context.startActivity(mIntent);
                        }
                        break;
                    case Constant.MESSAGE_LOGIN://其他设备登录
                    case Constant.MESSAGE_V_SUCCESS://加v成功
                    case Constant.MESSAGE_REMOVE_PENPAL://被移除笔友会
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
